﻿$(document).ready(function () {

    $("#JoinedDate").datepicker({ dateFormat: 'dd-mm-yy' }).val();;

    var loading = $("[id='loading']");
    loading.hide();

    var first = $("[id$='FirstName']");
    var middle = $("[id$='MiddleName']");
    var last = $("[id$='LastName']");
    var title = $("[id$='Title']");
    var position = $("[id$='Position']");
    var joindate = $("#JoinedDate");
    var empid = $("[id$='EmployeeID']");
    var id = $("#hfID");
    var image = $("#photo");

    $(document).on('click', '.edit', function () {

        var id = $(this).attr("name");
        $.ajaxSetup({ cache: false });

        $.getJSON("DataProcessor.aspx?function=getEmpId&id=" + id, function (json) {

            first.val(json.FirstName);
            middle.val(json.MiddleName);
            last.val(json.LastName);
            title.val(json.Title);
            position.val(json.Position);
            joindate.val(json.JoinedDate.slice(0, 10));
            empid.val(json.EmployeeID);
            image.attr("src", "GetImage.aspx?id=" + json.Id);
            $("#hfID").val(json.Id);

        });
    });

    $(document).on('click', '.delete', function () {

        var id = $(this).attr("name");

        if (confirm("Sure to delete?")) {
            $.ajaxSetup({ cache: false });

            $.getJSON("DataProcessor.aspx?function=delEmp&id=" + id, function (json) {

                if (json.flag == true) {
                    $("#divMsg").html("<p><img src='/image/success.png' height='48' width='48' style='vertical-align: middle;' /> " + json.message + "</p>");
                } else {
                    $("#divMsg").html("<p><img src='/image/error.png' height='48' width='48' style='vertical-align: middle;' /> " + json.message + "</p>");
                }

            });
        }
    });


    $(".btnLoadList").click(function () {

        $.ajaxSetup({ cache: false });
        $.getJSON("DataProcessor.aspx?function=getEmpList", function (obj) {

            // clear table first
            $("#tbEmpList").find("tr:gt(0)").remove();

            var rec = "";
            var i = 0;
            $.each(obj, function (key, value) {
                i = key + 1;
                var titlemsg;
                if (value.Title == 1) {
                    titlemsg = "Mr";
                } else {
                    if (value.Title == 2) {
                        titlemsg = "Mrs";
                    } else {
                        titlemsg = "Ms";
                    }
                }

                rec += "<tr><td>" + i + "</td><td>" + value.FirstName + " " + value.MiddleName + " " + value.LastName + "</td>";
                rec += "<td>" + titlemsg + "</td><td>" + value.Position + "</td><td>" + value.JoinedDate.slice(0, 10) + "</td><td>" + value.EmployeeID + "</td><td><button type='button' class='edit' name='" + value.Id + "'>Edit</button>&nbsp;<button type='button' class='delete' name='" + value.Id + "'>Delete</button></td></tr>";
            });

            $('#tbEmpList tr:last').after(rec);
        });

    });

    $("#btnSubmit").click(function () {

        if (first.val().length == 0) {
            alert("First name is required.");
            return false;
        }

        if (position.val().length == 0) {
            alert("Position details is required.");
            return false;
        }

        if (joindate.val().length == "0") {
            alert("Joined date is required");
            return false;
        }

        if (confirm("Sure to submit?")) {
            var json = "{'FirstName':'" + escape(first.val()) + "','MiddleName':'" + middle.val() + "','LastName':'" + last.val() + "','Title':'" + title.val() + "','Position':'" + position.val() + "','JoinedDate':'" + joindate.val() + "','EmployeeID':'" + empid.val() + "','id':'" + id.val() + "'}";
            var update = "registered";

            var ajaxPage = "";
            if ($("#hfID").val() != "") {
                ajaxPage = "DataProcessor.aspx?Save=2";
                update = "updated";
            }
            else
                ajaxPage = "DataProcessor.aspx?Save=1";

            $.ajax({

                type: "POST",
                url: ajaxPage,
                data: json,
                success: function (response) {
                    $("#divMsg").html("<p><img src='/image/success.png' height='48' width='48' style='vertical-align: middle;' /> Employee records successfully " + update + "</p>");
                },
                error: function (obj) {
                    $("#divMsg").html("<p><img src='/image/error.png' height='48' width='48' style='vertical-align: middle;' /> Error in inserting record</p>");
                },

            });
        }

    });

    $(document).ajaxStart(function () {
        loading.show();
    }).ajaxStop(function () {
        loading.hide();
    });
});